import sys, os, os.path, time, re
from market.models import UserProfile
from market.chat import chatSettings
from market.chat import xmpp
from urlparse import urlparse
from market.chat.httpb import HttpbParse
from market.chat import domish
from market.chat.Jid import internJID
from market.chat.xmpp_stringprep import nodeprep, resourceprep, nameprep
from xmpp.protocol import *
from market.models import WebSiteComment


####################################################################################
def sendOnBetroomChat( betroom, senderName, message ):
    try:
        # Create the betroom acount. The messages will be sent to the player using this account
        jid = xmpp.protocol.JID( chatSettings.CELIBAX_BETROOM_CHAT_ACCOUNT + chatSettings.CHAT_ADDRESS_EXTENSION + '/' + betroom.name )
        cli = xmpp.Client( jid.getDomain(), debug=[])
        cli.connect()
    
    
        # create the chat room   
        #http://xmpp.org/extensions/xep-0045.html#createroom
        #http://xmpppy.sourceforge.net/apidocs/
        cl = xmpp.Client( jid.getDomain(), debug=[] )
        cl.connect()
        cl.auth( jid.getNode(), chatSettings.CELIBAX_BETROOM_CHAT_PWD, resource=jid.getResource() )

        #http://xmpp.org/extensions/xep-0045.html#enter-muc
        chatroomJid = betroom.name + chatSettings.CHATROOM_ADDRESS_EXTENSION
        chatroomJidWithNickName = chatroomJid + '/'  + senderName
        #send a presence: http://stackoverflow.com/questions/11203921/how-to-send-message-to-a-chatroom-using-xmpppy
        presence = xmpp.Presence( to = chatroomJidWithNickName )
        presence.setTag('x',namespace=NS_MUC)
        cl.send( presence )
        # send the message
        # remove the time stamp
        msg = message[ (message.find(',') + 1): ]
        cl.send( xmpp.Message( chatroomJid, msg, typ='groupchat' ) )
        cl.disconnect()
    except:
        return



    
